package cn.ctbu.xiaopeng.studentscoremanage.service;


import cn.ctbu.xiaopeng.studentscoremanage.anno.Log;
import cn.ctbu.xiaopeng.studentscoremanage.dao.TeacherRepository;
import cn.ctbu.xiaopeng.studentscoremanage.enums.ResultEnum;
import cn.ctbu.xiaopeng.studentscoremanage.exception.ResultException;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.dto.TeacherQueryDTO;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.entity.Teacher;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.vo.TeacherPageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiaopeng
 * @version 1.0
 */
@Service
@RequiredArgsConstructor
public class TeacherService {

    private final TeacherRepository teacherRepository;

    /**
     * 查询所有教师信息列表
     *
     * @return
     */
    @Log

    public List<Teacher> getAll() {
        return teacherRepository.findAll();
    }

    /**
     * 根据id查询教师
     *
     * @param id
     * @return
     */
    @Log
    public Teacher findById(Integer id) {
        return teacherRepository.findById(id).orElse(null); //orElse(null) 如果查询不到返回null
    }

    /**
     * 根据姓名模糊查询 (like) 需要传递 刘%
     *
     * @param name
     * @return
     */
    @Log
    public List<Teacher> findByNameLike(String name) {
        return teacherRepository.findByNameLike(name);
    }

    /**
     * 根据姓名模糊查询 (like) 可直接 刘..
     *
     * @param name
     * @return
     */
    @Log
    public List<Teacher> findByNameStartWith(String name) {
        return teacherRepository.findByNameStartingWith(name);
    }

    /**
     * 根据姓名和密码查询, 用于登录校验
     *
     * @param name
     * @param password
     * @return
     */
    @Log
    public List<Teacher> findByNameAndPassword(String name, String password) {
        return teacherRepository.findByNameAndPassword(name, password);
    }

    /**
     * 添加教师
     *
     * @param teacher
     */
    @Log
    public Teacher insert(Teacher teacher) {
        return teacherRepository.save(teacher);
    }


    /**
     * 修改教师信息
     *
     * @param teacher
     */
    @Log
    public Teacher update(Teacher teacher) {
        return teacherRepository.save(teacher);
    }

    /**
     * 删除教师
     *
     * @param id
     */
    @Log
    public void deleteById(Integer id) {
        teacherRepository.deleteById(id);

    }

    /**
     * 根据id批量删除教师
     *
     * @param ids
     */
    @Log
    @Transactional
    public void deleteTeachersByIds(List<Integer> ids) {
        if (ids != null && !ids.isEmpty()) {
            teacherRepository.deleteByIds(ids);
        } else {
            throw new ResultException("教师ID列表不能为空");
        }
    }

    /**
     * 登录验证-验证学号和密码是否匹配
     *
     * @param sno
     * @param password
     * @return
     */
    public Teacher validateTnoAndPsw(String sno, String password) throws Exception {
        Teacher teacher = teacherRepository.findByTno(sno);
        if (teacher != null && teacher.getPassword().equals(password)) {
            //匹配
            return teacher;
        } else {
            throw new ResultException(ResultEnum.LOGIN_ERROR);
        }
    }

    /**
     * 分页查询
     *
     * @param pageable
     * @return
     */
    @Log
    public Page<Teacher> getAllWithPage(Pageable pageable) {
        return teacherRepository.findAll(pageable);
    }

    /**
     * 分页查询
     *
     * @param example
     * @param pageable
     * @return
     */
    @Log
    public Page<Teacher> getAllWithPage(Example<Teacher> example, Pageable pageable) {
        return teacherRepository.findAll(example, pageable);
    }

    /**
     * 条件分页查询
     * @param teacherQueryDTO
     * @param pageable
     * @return
     */
    @Log
    public Page<TeacherPageVO> getTeacherPageByConditions(TeacherQueryDTO teacherQueryDTO, Pageable pageable){
        return teacherRepository.getTeacherPageByConditions(teacherQueryDTO, pageable);
    }

/*    *//**
     * 查询具有最大工号的教师集合
     *
     * @return
     *//*
    @Log
    public List<Teacher> findMaxTno() {
        List<Teacher> teachers = getAllWithPage();
        //定义一个变量，用于存储最大工号
        String maxTnoByYear = "";
        for (Teacher teacher : teachers) {
            //提取教师工号的前四位
            String tnoYear = teacher.getTno().substring(0, 4);
            if (tnoYear.compareTo(maxTnoByYear) > 0) {
                maxTnoByYear = tnoYear;
            }
        }

        //根据最大工号的前四位查询教师集合
        return teacherRepository.findByTnoStartingWith(maxTnoByYear);
    }*/


}
